package com.jueecy.controller;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {

    @Value("${pro.uploadPath}")
    private String uploadPath;

    private static final String filePath= "/goods_img/";
    private static final String userImgUrl= "/user_img/";
    private static final String userChatImg= "/user_weChat_img";
    private static final Logger log= LoggerFactory.getLogger(FileController.class);


    //用户上传商品图片
    @RequestMapping("/upload")
    public Map<String,Object> upload(@RequestParam("file")MultipartFile file){
        Map map = new HashMap<String,Object>();
        try {
            if(file.isEmpty()){
                map.put("goods_picture",null);
                map.put("code",0);
                return map;
            }
            String fileName=file.getOriginalFilename();
            log.info("上传的文件名为:"+fileName);
            //设置文件存储路径
            String path=uploadPath+filePath;
            File dest = new File(new File(path).getAbsolutePath() + "/" + fileName);
            //检测目录是否存在
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();
            }
            //文件写入
            file.transferTo(dest);
            map.put("goods_picture",dest);
            map.put("code",200);
        }catch (Exception e){
            map.put("goods_picture",null);
            map.put("code",0);
            e.printStackTrace();
        }
        return map;
    }


    //用户上传头像
    @RequestMapping("/userImgUpload")
    public Map<String,Object> userImgUpload(@RequestParam("file")MultipartFile file){
        Map map = new HashMap<String,Object>();
        try {
            if(file.isEmpty()){
                map.put("user_img",null);
                map.put("code",0);
                return map;
            }
            String fileName=file.getOriginalFilename();
            log.info("上传的文件名为:"+fileName);
            //设置文件存储路径
            String path=uploadPath+userImgUrl;
            File dest = new File(new File(path).getAbsolutePath() + "/" + fileName);
            //检测目录是否存在
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();
            }
            //文件写入
            file.transferTo(dest);
            map.put("user_img",dest);
            map.put("code",200);
        }catch (Exception e){
            map.put("user_img",null);
            map.put("code",0);
            e.printStackTrace();
        }
        return map;
    }



    //用户上传微信
    @RequestMapping("/userChatUpload")
    public Map<String,Object> userChatUpload(@RequestParam("file")MultipartFile file){
        Map map = new HashMap<String,Object>();
        try {
            if(file.isEmpty()){
                map.put("user_chat_img",null);
                map.put("code",0);
                return map;
            }
            String fileName=file.getOriginalFilename();
            log.info("上传的文件名为:"+fileName);
            //设置文件存储路径
            String path=uploadPath+userChatImg;
            File dest = new File(new File(path).getAbsolutePath() + "/" + fileName);
            //检测目录是否存在
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();
            }
            //文件写入
            file.transferTo(dest);
            map.put("user_chat_img",dest);
            map.put("code",200);
        }catch (Exception e){
            map.put("user_chat_img",null);
            map.put("code",0);
            e.printStackTrace();
        }
        return map;
    }




}
